package com.xjwlmq.dx.dao.impl;

import com.xjwlmq.dx.dao.StudentBaseDao;
import com.xjwlmq.dx.entity.Student;
import com.xjwlmq.dx.util.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;

/**
 * @ClassName StudentDaoImpl
 * @Description 学生数据库操作实现
 * @Author 12994
 * @Date 2024/9/25 13:04
 * @Version 1.0
 **/
public class StudentDaoImpl extends BaseDao implements StudentBaseDao {
    @Override
    public ArrayList<Student> getStudentList() {
        ArrayList<Student> students = new ArrayList<>();
        Student thisStudent = null;
        try {
            this.rs = baseQuery("SELECT * FROM student;");
            if (this.rs == null) {
                logger.warn("查询结果为空!");
                throw new NullPointerException("查询结果为空!");
            }
            while (this.rs.next()) {
                thisStudent = new Student();
                thisStudent.setStudentNo(this.rs.getInt("StudentNo"));
                thisStudent.setLoginPwd(this.rs.getString("LoginPwd"));
                thisStudent.setStudentName(this.rs.getString("StudentName"));
                thisStudent.setSex(this.rs.getInt("Sex"));
                thisStudent.setGradeId(this.rs.getInt("GradeId"));
                thisStudent.setPhone(this.rs.getString("Phone"));
                thisStudent.setAddress(this.rs.getString("Address"));
                thisStudent.setBornDate(this.rs.getString("BornDate"));
                // System.out.println(this.rs.getTimestamp("BornDate"));
                // 注意想要返回日期+时间的话，需要sql的类型getTimestamp
                thisStudent.setEmail(this.rs.getString("Email"));
                thisStudent.setIdentityCard(this.rs.getString("IdentityCard"));
                students.add(thisStudent);
            }
        } catch (SQLException e) {
            logger.warn(String.valueOf(e.fillInStackTrace()));
        }
        return students;
    }

    @Override
    public ArrayList<Student> getStudentList(int studentNo) {
        ArrayList<Student> students = new ArrayList<>();
        Student thisStudent = null;
        try {
            this.rs = baseQuery("SELECT * FROM student WHERE StudentNo = ?;", studentNo);
            if (this.rs == null) {
                logger.warn("查询结果为空!");
                throw new NullPointerException("查询结果为空!");
            }
            while (this.rs.next()) {
                thisStudent = new Student();
                thisStudent.setStudentNo(this.rs.getInt("StudentNo"));
                thisStudent.setLoginPwd(this.rs.getString("LoginPwd"));
                thisStudent.setStudentName(this.rs.getString("StudentName"));
                thisStudent.setSex(this.rs.getInt("Sex"));
                thisStudent.setGradeId(this.rs.getInt("GradeId"));
                thisStudent.setPhone(this.rs.getString("Phone"));
                thisStudent.setAddress(this.rs.getString("Address"));
                thisStudent.setBornDate(this.rs.getString("BornDate"));
                // System.out.println(this.rs.getTimestamp("BornDate"));
                // 注意想要返回日期+时间的话，需要sql的类型getTimestamp
                thisStudent.setEmail(this.rs.getString("Email"));
                thisStudent.setIdentityCard(this.rs.getString("IdentityCard"));
                students.add(thisStudent);
            }
        } catch (SQLException e) {
            logger.warn(String.valueOf(e.fillInStackTrace()));
        }
        return students;
    }

    @Override
    public int addStudent(Student stu) {
        return this.baseUpdate("INSERT INTO student" +
                        " (StudentNo, LoginPwd, StudentName, Sex, GradeId" +
                        ", Phone, Address, BornDate, Email, IdentityCard) " +
                        "VALUES (null,?,?,?,?,?,?,?,?,?);", stu.getLoginPwd(), stu.getStudentName(),
                stu.getSex(), stu.getGradeId(), stu.getPhone(), stu.getAddress(),
                stu.getBornDate(), stu.getEmail(), stu.getIdentityCard());
    }

    @Override
    public int deleteStudentViaIdentityCard(String IdentityCard) {
        return this.baseUpdate("DELETE FROM student WHERE IdentityCard=?;", IdentityCard);
    }

    @Override
    public int deleteStudentViaStudentNo(int studentNo) {
        return this.baseUpdate("DELETE FROM student WHERE studentNo=?;", studentNo);
    }

    @Override
    public int updateStudentViaStudentNo(int studentNo, Student newStu) {
        System.out.println(newStu);
        if (studentNo != newStu.getStudentNo()) {
            logger.warn("要更新的学生学号不符!");
            throw new NullPointerException("要更新的学生学号不符!");
        }
        return this.baseUpdate("UPDATE student SET " +
                        "LoginPwd=?,StudentName=?,Sex=?,GradeId=?,Phone=?," +
                        "Address=?,BornDate=?,Email=?,IdentityCard=? " +
                        "where StudentNo=?", newStu.getLoginPwd(), newStu.getStudentName(),
                newStu.getSex(), newStu.getGradeId(), newStu.getPhone(), newStu.getAddress(),
                newStu.getBornDate(), newStu.getEmail(), newStu.getIdentityCard(), studentNo);
    }

    public static void main(String[] args) {
        new StudentDaoImpl().getStudentList().forEach(p -> {
//            logger.info(p.toString());
        });
        new StudentDaoImpl().getStudentList().forEach(System.out::println);
        //  可以转换ArrayList为Iterable -> 可以使用while
//        Iterator s=students.listIterator();
//        while (s.hasNext()){
//            System.out.println(s.next());
//        }

//
//        new StudentDaoImpl().getStudentList();
        System.out.println("________________________");
//
//        Student student1 = new Student();
//        student1.setLoginPwd("123456");
//        student1.setStudentName("XBS");
//        student1.setSex(1);
//        student1.setGradeId(1);
//        student1.setPhone("13103711234");
//        student1.setAddress("新疆乌鲁木齐");
//        student1.setBornDate(new Timestamp(System.currentTimeMillis()));
//        student1.setEmail("987654@gmail.com");
//        student1.setIdentityCard("123456654123654563");
//
//        System.out.printf("影响了%d行.\n", new StudentDaoImpl().addStudent(student1));
//        System.out.println("________________________");
//        new StudentDaoImpl().getStudentList();
//
//        System.out.printf("影响了%d行.\n", new StudentDaoImpl().deleteStudentViaIdentityCard("123456654123654563"));
//        System.out.println("________________________");
//        new StudentDaoImpl().getStudentList();

//        Student student2 = new Student();
//        student2.setStudentNo(1023);
//        student2.setLoginPwd("123456");
//        student2.setStudentName("XBS1");
//        student2.setSex(1);
//        student2.setGradeId(1);
//        student2.setPhone("13103711234");
//        student2.setAddress("新疆乌鲁木齐");
//        student2.setBornDate(new Date(System.currentTimeMillis()));
//        student2.setEmail("987654@gmail.com");
//        student2.setIdentityCard("123456654123654569");
//        System.out.printf("影响了%d行.\n", new StudentDaoImpl().updateStudentViaStudentNo(1023, student2));
//        System.out.println("________________________");
//        new StudentDaoImpl().getStudentList();
    }
}
